define(['jquery','mylayer','loginReg'], function ($,mylayer,md_loginReg) {
	var app={
		//点赞、倒彩
		interact:function(){
			if($(this).hasClass("interact")){
				var id=$(this).attr("data-id");
				var t=$(this).attr("t");
				var that=this;
				$.postAjax("/post/interact",{'t':t,'id':id},function(rst){
					if(rst.code=="0x00000"){
						var interact_value=$(that).find(".text");
						var value=parseInt(interact_value.html())?parseInt(interact_value.html()):0;
						interact_value.html(value+1);
						$(that).addClass('is_interact');
					}
					else if(rst.code=="0x00003"){
						var interact_value=$(that).find(".text");
						var value=parseInt(interact_value.html())?parseInt(interact_value.html()):0;
						if(value>0){
							interact_value.html(value-1);
							$(that).removeClass('is_interact');
						}
					}
					else{
						if(rst.result){
							mylayer.showMessage(rst.result);
						}
						if(rst.code=="0x00002"){
							$(that).addClass('is_interact');
						}
					}
				});
			}
		},
		//添加到收藏夹
		wish:function(){
			if($(this).hasClass("post_wish")){
				var id=$(this).attr("data-id");
				var that=this;
				$.postAjax("/wish/add",{'id':id},function(rst){
					if(rst.code=="0x00001"){
						md_loginReg.showLogin();//显示登录
						//设置登录完成事件
						md_loginReg.initSuccess(function(){
							$(that)[0].click();
						});
						return false;
					}
					else if(rst.code=="0x00000"){
						$(that).addClass('is_wish');
						var value_obj=$(that).find(".text");
						var value=parseInt(value_obj.html())?parseInt(value_obj.html()):0;
						value_obj.html(value+1);

					}
					else if(rst.code=="0x00003"){
						$(that).removeClass('is_wish');
						var value_obj=$(that).find(".text");
						var value=parseInt(value_obj.html())?parseInt(value_obj.html()):0;
						if(value>0){
							value_obj.html(value-1);
						}
					}
				});
			}
		},
		//分享统计
		interact_share:function(id,sns){
			$.postAjax("/share/interact",{'interact':'share','id':id,'sns':sns},function(rst){});
		},
		showDetail:function(){
			if($(this).hasClass("showDetail")){
				//显示分享详情
				var index=mylayer.showLoad(true);
				var share_id=$(this).attr("data-id");
				var site_url=window.location.href;
				var time_info={
					'view_time':0,
					'viewInterval':null
				}
				var that=this;
				var loadPost=function(md_post_detail){
					$.postAjax("/post/index",{'id':share_id},function(result){
						mylayer.closeLayer(index);
						$(".overlay_layer_post_detail").remove();
						$(".layer_post_detail").remove();
						var lay=mylayer.init({
							title:'',
							content:result.data,
							isHeight:true,
							closeFixed:true,
							phoneCover:true,
							success:function(){
								$("html,body").css("overflow-y","hidden");
								if(typeof md_post_detail!="undefined"){
									md_post_detail.init();
								}
								if(result.bulletin_flow_id){
									//统计停留时间定时
									time_info.viewInterval=setInterval(function(){
										time_info.view_time=time_info.view_time+1;
									},10);
								}
								if(typeof ga!="undefined"){
									ga('send', 'pageview', {
								    	'page': "/blog/"+share_id,
								    	'title': result.title?result.title:''
									});
								}
							},
							closeEvent:function(){
								history.pushState({ title: document.title,url:site_url}, document.title,site_url);
								if(result.bulletin_flow_id){
									//统计停留时间
									$.postAjax("/share/stay_time",{'share_id':share_id,'stay_time':time_info.view_time/100,'bulletin_flow_id':result.bulletin_flow_id},function(){});
								}
								//清除统计停留时间定时器
								clearInterval(time_info.viewInterval);
							},
							class_name:"layer_post_detail"
						});
						if (history.pushState){
							history.pushState({url:"/blog/"+share_id},'', "/blog/"+share_id);
							$(window).on('popstate', function(e){
							    mylayer.closeLayer(lay);
							    history.pushState({ title: document.title,url:site_url}, document.title,site_url);
							});
						}
						$("#layerBox"+lay).css({'top':"10px","margin-top":"0px"});
						//统计点击数量
						$.postAjax("/ajax/point",{id:share_id},function(){});
					});
				}	
				require(['post_detail'], function (md_post_detail){
					loadPost(md_post_detail);
			　　});
			}
		}
	}
	return app;
});
